import httpInstance from "@/utils/http";
import qs from "qs";

//获取所有的用户列表+根据用户名查询相关的用户信息
export function getUserAllAPI(
  current: number,
  pageSize: number,
  query: string
) {
return httpInstance({
    url: "/user/get/all",
    method: "POST",
    data: {
      current,
      pageSize,
      query,
    },
  });
}

//获取所有申请用户列表,根据id查询相关的申请信息
export function getUserApplicationListAPI(
  current: number,
  pageSize: number,
  query: number
) {
  return httpInstance({
    url: "/user-application/listAll",
    method: "POST",
    data: {
      current,
      pageSize,
      query,
    },
  });
}
//新增用户申请
export function addUserApplicationAPI(
  userId: number,
  reason: string,
  type: number,
  additionalInfo: number
) {
  return httpInstance({
    url: "/user-application/add",
    method: "POST",
    data: {
      userId,
      reason,
      type,
      additionalInfo,
    },
  });
}
//拒绝用户申请
export function rejectUserApplicationAPI(id: string) {
  return httpInstance({
    url: `/user-application/reject/${id}`,
    method: "POST",
    data: {},
  });
}

//同意用户申请
interface AcceptParams {
  id: number;
  userId: number;
  type: number;
  add?: string;
}
export function acceptUserApplicationAPI(params: AcceptParams) {
  return httpInstance({
    url: "/user-application/accept",
    method: "POST",
    data: params,
  });
}

//批量删除用户申请
export function deleteUserApplicationAPI(ids: number[]) {
  return httpInstance({
    url: "/user-application/delete",
    method: "DELETE",
    params: {
      ids,
    },
    paramsSerializer: (params) => {
      return qs.stringify(params, { arrayFormat: "repeat" }); // announcementIds=1&announcementIds=2
    },
  });
}


//批量删除用户
export function deleteUserAPI(userIds: string | string[]) {
  const idArray = Array.isArray(userIds) ? userIds : [userIds];

  return httpInstance({
    url: "/user/delete",
    method: "DELETE",
    params: {
      userIds: idArray,
    },
    paramsSerializer: {
      indexes: null,
    },
  });
}

//单个删除用户
export function deleteuserAPI(id:number) {

  return httpInstance({
    url: "/user/delete",
    method: "GET",
    params:{
      id
    },
  });
}


//修改用户信息
export const updateUserAPI = (data: {
  id: number;  // 用户ID（必填）
  userAccount: string;  // 用户账号（必填）
  userName: string;     // 用户名（必填）
  originUserPassword?: string; // 原密码（可选）
  newUserPassword?: string;    // 新密码（可选）
  checkedNewUserPassword?: string; // 确认新密码（可选）
  userAvatar?: string;         // 头像URL（可选）
  phoneNumber?: string;        // 手机号（可选）
  major?: string;              // 专业（可选）
  grade?: string;              // 年级（可选）
  userClass?: string;          // 班级（可选）
}) => {
  return httpInstance({
    url: '/user/update',
    method: 'POST',
    data,
    headers: {
      'Content-Type': 'application/json'
    }
  });
};

//新增用户
export const addUserAPI = (data: {
  userAccount: string;  // 用户账号（必填）
  userName: string;     // 用户名（必填）
  userPassword: string; // 密码（必填）
 checkedUserPassword: string; // 确认密码（必填）
  phoneNumber?: string;  // 手机号（可选）
  major?: string;        // 专业（可选）
  grade?: string;        // 年级（可选）
  userClass?: string;    // 班级（可选）
  userRole?: string;     // 用户角色（可选，默认'user'）
}) => {
  return httpInstance({
    url: '/user/add',
    method: 'POST',
    data,
    headers: {
      'Content-Type': 'application/json'
    }
  });
};

//上传头像

export const uploadAvatarAPI = (file: File, userId?: number) => {
  const formData = new FormData();
  formData.append('file', file);

  const params = userId ? { userId } : undefined;

  return httpInstance({
    url: '/upload-avatar',
    method: 'POST',
    data: formData,
    params,  // 查询参数会自动附加到URL
    // 移除手动设置的Content-Type头部
  });
};

//修改个人信息
export const updateUserEditAPI = (data: {
  id: number;
  userAccount?: string;
  originUserPassword?: string;
  newUserPassword?: string;
  checkedNewUserPassword?: string;
  userAvatar?: string;
  userName?: string;
}) => {
  return httpInstance({
    url: "/user/edit",
    method: "POST",
    data,
    headers: {
      "Content-Type": "application/json",
    },
  });
};
